package com.niit.mall.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.mall.common.domain.dto.PageDTO;
import com.niit.mall.common.utils.BeanUtils;
import com.niit.mall.user.domain.po.UserLoginLog;
import com.niit.mall.user.domain.vo.UserLoginLogVO;
import com.niit.mall.user.domain.vo.UserVO;
import com.niit.mall.user.mapper.UserLoginLogMapper;
import com.niit.mall.user.service.IUserLoginLogService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: pardo_mall_backend
 * @BelongsPackage: com.niit.mall.user.service.impl
 * @Author: XihenXi
 * @CreateTime: 2025-06-20  16:00
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class UserLoginLogServiceImpl extends ServiceImpl<UserLoginLogMapper, UserLoginLog> implements IUserLoginLogService {

    /**
     * 给分页添加用户登录日志
     * @param page page
     * @return PageDTO<UserVO>
     */
    @Override
    public PageDTO<UserVO> addLoginLog(PageDTO<UserVO> page) {
        // 获取用户列表
        List<UserVO> userList = page.getList();
        if (userList == null || userList.isEmpty()) {
            return page;
        }

        // 遍历用户，查询并设置最新登录日志
        for (UserVO userVO : userList) {
            if (userVO.getId() != null) {
                UserLoginLog log = baseMapper.selectOne(new QueryWrapper<UserLoginLog>().eq("user_id", userVO.getId()).orderByDesc("create_time").last("limit 1"));
                UserLoginLogVO logVO = BeanUtils.copyBean(log, UserLoginLogVO.class);
                userVO.setUserLoginLog(logVO);
            }
        }
        return page;
    }
}

